package com.ryl.yomall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.ryl.yomall.item.service.ItemService;
import com.ryl.yomall.list.client.ListFeignClient;
import com.ryl.yomall.model.product.BaseCategoryView;
import com.ryl.yomall.model.product.SkuInfo;
import com.ryl.yomall.model.product.SpuSaleAttr;
import com.ryl.yomall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 * @author 我的世界
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> result = new HashMap<> ();
        
        return result;
    }

    @Override
    public Map getItem(Long skuId) {
        Map map = new HashMap();
        CompletableFuture<SkuInfo> completableFuture = CompletableFuture.supplyAsync ( () -> {
            // 1. 获取skuInfo 信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo ( skuId );
            if (skuInfo != null) {
                map.put ( "skuInfo" ,skuInfo );
            }
            return skuInfo;
        } ,threadPoolExecutor );
        CompletableFuture<Void> categoryView1 = completableFuture.thenAcceptAsync ( (skuInfo) -> {
            if (skuInfo != null) {
                BaseCategoryView categoryView = productFeignClient.getBaseCategoryView ( skuInfo.getCategory3Id ( ) );
                map.put ( "categoryView" ,categoryView );
            }
        } ,threadPoolExecutor );
        CompletableFuture<Void> price = CompletableFuture.runAsync ( () -> {
            BigDecimal skuPrice = productFeignClient.getPrice ( skuId );
            map.put ( "price" ,skuPrice );
        } ,threadPoolExecutor );
        CompletableFuture<Void> spuSaleAttrList = completableFuture.thenAcceptAsync ( (skuInfo) -> {
            if (skuInfo != null) {
                List<SpuSaleAttr> spuSaleAttrListCheckBySkuList = productFeignClient.getSpuSaleAttrAndChecked ( skuInfo.getSpuId ( ) ,skuInfo.getId ( ) );
                map.put ( "spuSaleAttrList" ,spuSaleAttrListCheckBySkuList );
            }
        } ,threadPoolExecutor );
        //组合对应的skuId
        CompletableFuture<Void> valuesSkuJson = completableFuture.thenAcceptAsync ( (skuInfo) -> {
            if (skuInfo != null) {
                Map skuValueIdsMap = productFeignClient.getSaleAttrValuesBySpu ( skuInfo.getSpuId ( ) );
                map.put ( "valuesSkuJson" ,JSON.toJSONString ( skuValueIdsMap ) );
            }
        } ,threadPoolExecutor );
        // 6.远程调用更新热度
        CompletableFuture.runAsync ( () ->{
            listFeignClient.hotScore ( skuId );
        },threadPoolExecutor );
        //等待线程全部执行完
        CompletableFuture.allOf (valuesSkuJson,spuSaleAttrList ,price,categoryView1,completableFuture ).join ();
        return map;
    }
}
